package com.github.aloxc.plugin.restplus.entity;

import com.github.aloxc.plugin.DonotGuard;
import com.intellij.ide.util.PropertiesComponent;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public enum Sort {
    /**
     * 降序
     */
    @DonotGuard
    DESC("desc"),

    /**
     * 升序
     */
    @DonotGuard
    ASC("asc");
    private Sort(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    private String type;

    private static Map<String, Sort> SORT_MAP = Arrays.stream(Sort.values()).collect(Collectors.toMap(k->k.getType().toLowerCase(), v->v));
    public static Sort getByType(String type){
        if(type == null){
            return ASC;
        }
        return SORT_MAP.get(type.toLowerCase());
    }

    public static Sort get(){
        PropertiesComponent instance = PropertiesComponent.getInstance();
        String sort = instance.getValue(Sort.class.getName());
        return getByType(sort);
    }

    public static void set(Sort sort){
        PropertiesComponent instance = PropertiesComponent.getInstance();
        instance.setValue(Sort.class.getName(),sort.getType());
    }
}
